package com.zy.explore.common.infrastructure.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * Knife4j配置类
 *
 * @author ChinaYin  主页:https://gitee.com/zy-explorej2ee 邮箱:zyexplorej2ee@sina.com
 * @date 2021年06月13日 14:56:15
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile("!prod")
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfig {

	@Resource
	private OpenApiExtensionResolver openApiExtensionResolver;

	/**
	 * 所有接口
	 */
	@Bean(value = "apiAll")
	public Docket apiAll() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.groupName("所有接口")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.zy.explore"))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions("所有接口"));
	}
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("项目接口说明文档")
				.contact(new Contact("ZY", "https://gitee.com/zy-explorej2ee", "zyexplorej2ee@sina.com"))
				.description("基于Swagger2和Knife4j实现的接口文档")
				.version("1.0")
				.build();
	}

	/**
	 * API分组 示例模块
	 */
	@Bean(value = "apiV1")
	public Docket apiV1() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo1())
				.groupName("模块:示例接口")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.zy.explore.common.interfaces.facade.v1.example"))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions("模块:示例接口"));
	}
	private ApiInfo apiInfo1() {
		return new ApiInfoBuilder()
				.title("示例模块说明文档")
				.contact(new Contact("ChinaYin", "https://gitee.com/zy-explorej2ee", "zyexplorej2ee@sina.com"))
				.description("基于Swagger2和Knife4j实现的接口文档")
				.version("1.0")
				.build();
	}

	/**
	 * API分组 系统模块
	 */
	@Bean(value = "apiV2")
	public Docket apiV2() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo2())
				.groupName("模块:系统接口")
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.zy.explore.sys.interfaces.facade.v1"))
				.paths(PathSelectors.any())
				.build().extensions(openApiExtensionResolver.buildExtensions("模块:系统接口"));
	}
	private ApiInfo apiInfo2() {
		return new ApiInfoBuilder()
				.title("系统模块说明文档")
				.contact(new Contact("ChinaYin", "https://gitee.com/zy-explorej2ee", "zyexplorej2ee@sina.com"))
				.description("基于Swagger2和Knife4j实现的接口文档")
				.version("1.0")
				.build();
	}
}